<template>
  <div class="searchForm-container">
    <div class="search-form">
      <el-form
        :label-width="searchOptions.labelWidth"
        label-position="right"
        ref="formRef"
      >
        <el-row :gutter="15">
          <el-col
            ref="colRef"
            :span="item.search?.el !== 'date' ? 8 : 16"
            v-for="item in searchOptions.columns"
            :key="item.prop"
            clearable
          >
            <el-form-item :label="item.label" v-if="!item.searchSlot">
              <el-input
                v-if="item.search?.el == 'input'"
                v-model="search1[item.prop]"
                :placeholder="`请输入${item.label}`"
                clearable
              ></el-input>
              <el-select
                v-if="item.search?.el == 'select'"
                v-model="search1[item.prop]"
                :placeholder="`请选择${item.label}`"
                clearable
              >
                <el-option
                  v-for="select in item.search.options.data"
                  :key="select.value"
                  :label="select.label"
                  :value="select.value"
                ></el-option>
              </el-select>
              <el-date-picker
                v-if="item.search?.el == 'date'"
                v-model="search1[item.prop]"
                :type="item.search.options.type"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                clearable
              ></el-date-picker>
            </el-form-item>

            <slot v-if="item.searchSlot" :name="`${item.prop}Search`"></slot>
          </el-col>

          <slot name="searchExpand"></slot>
        </el-row>
      </el-form>
    </div>

    <div class="search-btn">
      <el-button type="primary" @click="handleSearch">搜索</el-button>
      <el-button @click="handleInit">重置</el-button>
      <el-button type="primary" link @click="handleExpand" style="height: 32px">
        {{ collapsed ? "展开" : "合并" }}
        <el-icon class="el-icon--right">
          <component :is="collapsed ? ArrowDown : ArrowUp"></component>
        </el-icon>
      </el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { ProTableType } from "@/type/proTable";
import { ArrowDown, ArrowUp } from "@element-plus/icons-vue";
import { ELComponentInstance } from "@/type/components/element-plus";

const emits = defineEmits(["search", "init"]);

const collapsed = ref(true);

defineProps<{
  searchOptions: ProTableType;
  search1: any;
}>();

const formRef = ref<ELComponentInstance["formRef"]>();

const colRef = ref<ELComponentInstance["colRef"]>();

onMounted(() => {
  handleInitExpand()
});

// 展开合并
const handleExpand = () => {
  if (collapsed.value == false) {    
    collapsed.value = true;
    handleInitExpand()
  }else{
    collapsed.value = false
    JSON.parse(JSON.stringify(colRef.value)).forEach((item:any,index:number) => {
      (document.querySelector(".search-form")?.querySelectorAll(".el-col")[index] as HTMLElement).style.display = "block";
  })}
};

// 初始化表单
const handleInitExpand = () => {
  let sum = 0;
  JSON.parse(JSON.stringify(colRef.value)).forEach(
      (item: any, index: number) => {
        sum = item.span + sum;
        if (sum > 24) {
          (
            document.querySelector(".search-form")?.querySelectorAll(".el-col")[
              index
            ] as HTMLElement
          ).style.display = "none";
        }
      }
    );
}

const handleSearch = () => {
  emits("search");
};

const handleInit = () => {
  emits("init");
};
</script>

<style lang="scss">
.searchForm-container {
  display: flex;
  justify-content: space-between;
  .search-form {
    width: 80%;
    .el-select {
      width: 100%;
      .el-input__wrapper {
        width: 100%;
      }
    }
  }
  .search-btn {
    width: 20%;
    height: 50px;
    display: flex;
    justify-content: end;
  }
}
</style>
